
local point_class = ac.point.__index
local unit_class = ac.unit.__index

--跳跃到指定点
function unit_class:jump(point,speed,height,func)
    local arc = 0.1
    if height<1 then
        arc = height
    end
    local mover = ac.mover('point'){
        mover = self,
        start = self,
        target = point,
        max_height = height,
        arc = arc,
        speed = speed,
        on_remove = func,
    }
    return mover
end

--向指定点/单位发射投射物
function point_class:launch_missile(target,model,speed,arc,func,data)
    local mover_class = target.type=='point' and target.type or 'target'
    local params = {
        start = self,
        target = target,
        model = model,
        arc = arc,
        speed = speed,
        need_remove_mover = true,
        auto_remove = false,
        on_finish = func,
        show_state = true,
    }
    for k,v in pairs(data or {}) do
        params[k] = v
    end
    local mover = ac.mover(mover_class)(params)
    return mover
end

function unit_class:launch_missile(target,model,speed,arc,func,data)
    local start = self:get_launch_point()
    return start:launch_missile(target,model,speed,arc,func,data)
end

--向指定方向波
function point_class:launch_wave(model,angle,speed,distance,size,height)
    local mover = ac.mover('line'){
        start = self,
        angle = angle,
        model = model,
        speed = speed,
        height = height,
        size = size,
        distance = distance,
    }
    return mover
end

function unit_class:launch_wave(model,angle,speed,distance,size,height)
    local start = self:get_point()
    return start:launch_wave(model,angle,speed,distance,size,height)
end

--贝塞尔曲线运动
function point_class:bezier_missile(target,center,model,speed,func)
    if type(center)=='table' and center.type~='point' then
        local point = self:get_point()
        local angle = point/target:get_point()
        local height = center[3] or 0
        if math.random(100)<=50 then
            center[1] = angle - center[1]
        else
            center[1] = angle + center[1]
        end
        center = self - center
        center.z = height + point:get_height()
    end
    return ac.mover('bezier'){
        start = self,
        model = model,
        target = target,
        center = center,
        speed = speed,
        on_finish = func,
    }
end

function unit_class:bezier_missile(target,center,model,speed,func)
    local start = self:get_launch_point()
    return start:bezier_missile(target,center,model,speed,func)
end


--对单位的选择优先级提升或者降低
function unit_class:priority_add(unit,name,count)
    local priority = self:get_data(name) or {}
    priority[unit.handle] = (priority[unit.handle] or 0) + count
    if priority[unit.handle]==0 then
        priority[unit.handle] = nil
    end
    self:set_data(name,priority)
end


--[分隔选取范围]
function unit_class:getUnitPoint(point,range,area)
    local group = {}
    local units = ac.selector('unit'):range(point,range):enemy(self):get()
    if #units==0 then
        return point
    end
    for a,unit in ipairs(units) do
        local x,y = unit:get_point():get()
        group[a] = {x=x,y=y}
    end
    local groups = sc.groupByHierarchical(group,area)
    local points = sc.getGroupsCenterPoints(groups)
    return points
end

--[选取范围内敌人最多的点]
function unit_class:maxUnitPoint(point,range,area)
    local points = self:getUnitPoint(point,range,area)
    local weight = {}
    for a,pt in ipairs(points) do
        weight[a] = {pt,pt.groupSize} 
    end
    local pt = table.get_by_weight(weight)
    return ac.point(pt.x,pt.y)
end

--选择敌人最多的地方
function unit_class:getEnemyPoint(speed)
    local speed = speed or 200
    local points = self:get_data('临时分割区域') or {}
    local time = self:get_data('临时分割时间',ac.clock())
    if #points==0 or time<=ac.clock() then
        time = ac.clock() + 500
        points = self:getUnitPoint(self,math.min(self:get('攻击距离')+50,900),150)
        self:set_data('临时分割时间',time)
        self:set_data('临时分割区域',points)
    end
    local info = table.remove(points,1)
    local base = self:get_point()
    if info then
        local area = (500 - (time - ac.clock()))/1000 * speed
        local point = ac.point(info.x,info.y)
        local angle = point / base
        return point - {angle,area}
    end
    return base
end

--[创建残影]
function unit_class:create_shadow(point,angle,time)
    local point = point or self
    local effect = point:get_point():effect{
        model = self:get_model(),
        angle = angle or self:get_facing(),
        time = time,
        color = {0,0,0,50},
        hide_when_remove = true,
    }
    return effect
end